/*
 * LocalRollUpVarianceXY.java
 *
 * Created on January 31, 2005, 2:31 PM
 */

package edu.temple.GUS.MMA.Functions;

import edu.temple.GUS.MMA.DataTypes.*;
import edu.temple.GUS.MMA.Exceptions.MMAFunctionInitException;
import edu.temple.GUS.MMA.util.StatisticalOperator;

/**
 *
 * @author  jeffleon
 */
public class LocalRollUpVarianceXY {
    
    /** Creates a new instance of LocalRollUpVarianceXY */
    public LocalRollUpVarianceXY() {}
    
    // Roll up a time cube in x and y dimension
    public MMATimeSeries execute( MMATimeCube tCube ) throws MMAFunctionInitException
    {
        if ( tCube == null)
            throw new MMAFunctionInitException(this.getClass().toString() + " Time cube is not initialized.");    

        // Initialze the dimensions of timecube and the noData value
        int x = tCube.getX();
        int y = tCube.getY();
        int t = tCube.getT();
        float noData = tCube.getNoData();

        MMATimeSeries outputTSeries = new MMATimeSeries(t);
        float resultData[] = new float[t];
        float tempX[] = new float[x];
        float tempY[] = new float[y];
        
        for ( int k=0; k <t; k++)
        {
            for ( int j=0; j <y; j++)
            {
                // Roll up in x-dimesion
                for ( int i=0; i <x; i++)
                    if (tCube.getElement(i,j,k) == noData)
                    {
                        resultData[k] = noData;
                        break;
                    }
                    else
                        tempX[i] = tCube.getElement(i,j,k);
                
                if (resultData[k] == noData)
                    break;
                else
                    tempY[j] = StatisticalOperator.variance(tempX,x);
            }
                
            if (resultData[k] != noData)
                resultData[k] = StatisticalOperator.variance(tempY,y);
        }
        
        outputTSeries.putData(resultData);
        outputTSeries.putNoData(noData);
        return outputTSeries;
    }
    
}
